package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.PageInfo;
import com.tanhua.dubbo.server.pojo.UserLike;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/3/1 22:06
 * @version: 0.0.1
 * @modified By:
 */
@Service(version = "1.0.0")
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * @description 保存喜欢
     * @author
     * @date
     * @param
     *@return
     */
    @Override
    public String saveUserLike(Long userId, Long likeUserId) {
        Query query=Query.query(Criteria.where("userId").is(userId).and("likeUserId")
                .is(likeUserId));
        long count = this.mongoTemplate.count(query, UserLike.class);
        if(count>0){
            return null;
        }
        UserLike userLike=new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeUserId);
        this.mongoTemplate.save(userLike);
        return userLike.getId().toHexString();
    }
    /**
     * @description 判断是否相互喜欢
     * @author
     * @date
     * @param
     *@return
     */
    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        Criteria criteria1=Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2=Criteria.where("likeUserId").is(userId).and("userId").is(likeUserId);
        Criteria criteria=new Criteria().orOperator(criteria1,criteria2);
        return this.mongoTemplate.count(Query.query(criteria),UserLike.class)==2;
    }
    /**
     * @description 删除喜欢
     * @author
     * @date
     * @param
     *@return
     */
    @Override
    public Boolean deleteUserLike(Long userId, Long likeUserId) {
        Query query=Query.query(Criteria.where("userId").is(userId).and("likeUserId")
        .is(likeUserId));
        DeleteResult deleteResult = this.mongoTemplate.remove(query, UserLike.class);
        return deleteResult.getDeletedCount()==1;
    }

    @Override
    public Long queryEachLikeCount(Long userId) {
        //思路：首先查询我的喜欢列表，然后，在我的喜欢的人的范围内，查询喜欢我的人
        //查询我的喜欢列表
        Query query=Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = this.mongoTemplate.find(query, UserLike.class);
        //收集到我的喜欢列表中的用户id(对方)
        List<Long>likeUserIds=new ArrayList<>();
        for (UserLike userLike : userLikes) {
            likeUserIds.add(userLike.getLikeUserId());
        }
        //在我的喜欢列表范围内，查询喜欢我的人有哪些
        Query query2=Query.query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId));
        return this.mongoTemplate.count(query2,UserLike.class);
    }

    @Override
    public Long queryLikeCount(Long userId) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        long count = this.mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public Long queryFanCount(Long userId) {
        Query query=Query.query(Criteria.where("likeUserId").is(userId));
        long count = this.mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public PageInfo<UserLike> queryEachLikeList(Long userId, Integer page, Integer pageSize) {
        PageInfo<UserLike>pageInfo=new PageInfo<>();
        pageInfo.setTotal(0);//暂时不提供
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        Query query=Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikeList = this.mongoTemplate.find(query, UserLike.class);
        List<Long>likeIds=new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            likeIds.add(userLike.getLikeUserId());
        }
        Pageable pageable= PageRequest.of(page-1,pageSize, Sort.by(Sort.Order.desc("created")));
        Query query2=Query.query(Criteria.where("userId").in(likeIds).and("likeUserId").is(userId));
        query2.with(pageable);
        List<UserLike> userLikes = this.mongoTemplate.find(query2, UserLike.class);
        pageInfo.setRecords(userLikes);
        return pageInfo;
    }

    @Override
    public PageInfo<UserLike> queryLikeList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest=PageRequest.of(page-1,pageSize,Sort.by(Sort.Order.desc("created")));
        Query query=Query.query(Criteria.where("userId").is(userId));
        query.with(pageRequest);
        List<UserLike> userLikeList = this.mongoTemplate.find(query, UserLike.class);
        PageInfo<UserLike>pageInfo=new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(userLikeList);
        return pageInfo;
    }

    @Override
    public PageInfo<UserLike> queryFanList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest=PageRequest.of(page-1,pageSize,Sort.by(Sort.Order.desc("created")));
        Query query=Query.query(Criteria.where("likeUserId").is(userId));
        query.with(pageRequest);
        List<UserLike> userLikeList = this.mongoTemplate.find(query, UserLike.class);
        PageInfo<UserLike>pageInfo=new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(userLikeList);
        return pageInfo;
    }
}
